<?php
/**
 * 商品
 */
namespace App;
use Model\GoodsMod;
use Pub\cache\Cache;

defined('IN_IA') or exit('Access Denied');

class Goods extends Action
{
    private $Goods = null;

    private $_goods_id = 0;
    public function __construct()
    {
        parent::__construct();
        $this->Goods = new GoodsMod();
        if(isset($this->_gpc["goods_id"])) $this->_goods_id = $this->_gpc["goods_id"];
    }

    public function __set($name, $value)
    {
        // TODO: Implement __set() method.
        $this->$name = $value;
    }

    /**
     * 库存和销量获取
     */
    public function inventoryAndSales()
    {
        return $this->Goods->findOne(
            array("id"=>$this->_goods_id),
            array("inventory","sales")
        );
    }

    /**
     * 添加新商品
     * @param data 数据集合
     */
    public function add()
    {
        $this->_gpc["data"]["uniacid"] = $this->_uniacid;
        $this->_gpc["data"]["images"] = json_encode($this->_gpc["data"]["images"]);
        if($this->Goods->insert($this->_gpc["data"])){
            $this->_result = array("status"=>"success","data"=>pdo_insertid());
        }
        $this->result($this->_result);
    }

    /**
     * 修改商品
     * @param goods_id 商品id
     * @param data 修改的数据集合
     */
    public function edit()
    {
        Cache::remove(Cache::GOODS,array("goods_id"=>$this->_goods_id));
        $this->_gpc["data"]["images"] = json_encode($this->_gpc["data"]["images"]);
        if($this->Goods->update($this->_gpc["data"],array("id"=>$this->_goods_id))){
            $this->_result = array("status"=>"success");
        }
        $this->result($this->_result);
    }

    /**
     * 获取请求数据的总数
     */
    public function getTotal()
    {
        $this->_result = array(
            "status"=>"success",
            "data"=>$this->Goods->total(
                array("uniacid"=>$this->_uniacid)
            )
        );
        $this->result($this->_result);
    }

    /**
     * 获取列表
     * @param limit 请求下标
     */
    public function getList()
    {
        $_list = $this->Goods->getList($this->_gpc["limit"],$this->_uniacid);
        if(count($_list) > 0){
            foreach($_list as $_key=>$_value){
                $_images = json_decode($_value["images"],true);
                $_list[$_key]["cover"] = tomedia($_images[0]);
            }
            unset($_images);
            $this->_result = array("status"=>"success","data"=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 获取商品详情
     * @param goods_id 商品id
     */
    public function getGoods()
    {
        $_goods = $this->Goods->findOne(array(
            "id"=>$this->_goods_id
        ),array("inventory","sales"));
        $_goods = array_merge($_goods,$this->findGoods());
        if($_goods) $this->_result = array("status"=>"success","data"=>$_goods);
        $this->result($this->_result);
    }

    /**
     * 获取商品详情，非接口
     * @param $_goods_id 商品id
     * @return array 数据
     */
    public function getGoods2($_goods_id)
    {
        $this->_goods_id = $_goods_id;
        return $this->findGoods();
    }

    /**
     * 获取一个商品详情
     */
    public function findGoods()
    {
        $_goods = Cache::get(Cache::GOODS,array("goods_id"=>$this->_goods_id));
        if(!$_goods || KUYUAN_DEBUG){
            $_goods = $this->Goods->findOne(array("id"=>$this->_goods_id),array("id","name","price","detail","images","weight","status"));
            if($_goods){
                $_goods["images"] = json_decode($_goods["images"],true);
                $_goods["images_src"] = array();
                foreach ($_goods["images"] as $_v){
                    $_goods["images_src"][] = tomedia($_v);
                }
                $_goods["detail"] = htmlspecialchars_decode($_goods["detail"]);
                Cache::set(Cache::GOODS,$_goods,array("goods_id"=>$this->_goods_id));
            }else{
                $_goods = false;
            }
        }
        return $_goods;
    }

    /**
     * 修改一个商品的状态
     * @param goods_id 商品id
     * @param status 修改的状态
     */
    public function changeStatus()
    {
        if($this->Goods->update(array("status"=>$this->_gpc['status']),array("id"=>$this->_goods_id))){
            $this->_result = array("status"=>"success");
        }
        $this->result($this->_result);
    }

    //---------APP-------

    /**
     * 前端商品列表
     */
    public function getListApp()
    {
        $_list = $this->Goods->findAll(
            array("status"=>1,"uniacid"=>$this->_uniacid),
            array(
                "order"=>array("weight","desc"),
                "limit"=>$this->_gpc["limit"]
            ),
            array("id","inventory","sales")
        );
        if(count($_list) > 0){
            foreach($_list as $_k=>$_v){
                $this->_goods_id = $_v["id"];
                $_list[$_k] = array_merge($_v,$this->findGoods());
            }
            $this->_result = array("status"=>"success","data"=>$_list);
        }
        $this->result($this->_result);
    }

    /**
     * 前端获取一个商品信息
     * @param goods_id 商品id
     */
    public function getGoodsApp()
    {
        $_goods = $this->Goods->findOne(array("id"=>$this->_goods_id,"status"=>1),array("inventory","sales"));
        if($_goods){
            $_goods = array_merge($_goods,$this->findGoods());
            $this->_result = array("status"=>"success","data"=>$_goods);
        }
        $this->result($this->_result);
    }
}